package com.design.pattern.observe.observe;

import com.design.pattern.observe.topic.Subject;
import com.design.pattern.observe.topic.WeatherData;

/**
 * @author wangyingjie
 * @date
 * @remark 温度、湿度布告板（当前状况布告板）
 */
public class CurrentConditionsDisplay implements Observe,DisplayElement {
    private float temp;
    private float humidity;
    private Subject subject;//订阅的主题

    /**
     * 构造时，订阅主题
     * @param subject
     */
    public CurrentConditionsDisplay(Subject subject) {
        this.subject = subject;
        subject.registeObserve(this);//订阅主题
    }

    /**
     * 显示
     */
    @Override
    public void display() {
        System.out.println("CurrentCondion：温度是【"+temp+"】，湿度是【"+humidity+"】。");
    }

    /**
     * 更新参数
     * @param temp 温度
     * @param humidity 湿度
     * @param pressure 压强
     */
    @Override
    public void update(float temp, float humidity, float pressure) {
        this.temp = temp;
        this.humidity = humidity;
        display();//更新完显示
    }

    /**
     * 订阅主题
     * @param subject
     */
    public void substribeTopic(Subject subject){
        //取消原有订阅
        removeTopic();
        this.subject = subject;
        subject.removeObserve(this);
    }

    /**
     * 取消订阅
     */
    public void removeTopic(){
        subject.removeObserve(this);
    }

    public static void main(String[] args) {
        WeatherData weatherData = new WeatherData();
        CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData);
        //当主题发生参数变更时，通知订阅此主题的观察者，改变参数变显示
        weatherData.setMessurement(1.11f,2.22f,3.33f);
    }
}
